package com.xqboss.apps.controller.system;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xqboss.apps.domain.consumer.ConsumerFamiliarProblem;
import com.xqboss.apps.domain.sys.SysFamiliarProblem;
import com.xqboss.apps.service.consumer.ConsumerFamiliarProblemService;
import com.xqboss.apps.service.sys.SysFamiliarProblemService;
import com.xqboss.apps.vo.sys.FamiliarProblemVo;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 常见问题接口
 *
 * @author wangcong
 * @date 2023-09-01
 */
@Api(tags = {"常见问题"})
@RestController
@RequestMapping("/apps/problems")
public class SysFamiliarProblemController extends BaseController {
    @Autowired
    private SysFamiliarProblemService sysFamiliarProblemService;
    @Autowired
    private ConsumerFamiliarProblemService consumerFamiliarProblemService;


    @ApiOperation("根据主键查询")
    @GetMapping("findById/{id}")
    public R<FamiliarProblemVo> findByKey(@PathVariable("id") Long id) {
        SysFamiliarProblem problem = sysFamiliarProblemService.getById(id);
        FamiliarProblemVo vo = BeanUtil.copyProperties(problem, FamiliarProblemVo.class);
        LambdaQueryWrapper<ConsumerFamiliarProblem> wp = new LambdaQueryWrapper<>();
        wp.eq(ConsumerFamiliarProblem::getUserId, getUserId());
        wp.eq(ConsumerFamiliarProblem::getProblemId, id);
        ConsumerFamiliarProblem one = consumerFamiliarProblemService.getOne(wp);

        if (ObjUtil.isNotNull(one)) {
            vo.setResolve(one.getResolve());
        }
        return R.ok(vo);
    }

    @ApiOperation("是否解决问题")
    @PostMapping("update")
    public R update(@ApiParam(name = "id", value = "常见问题id", required = true)
                    @RequestParam Long id,
                    @ApiParam(name = "isSolved", value = "是否解决 0否，1是", required = true)
                    @RequestParam Integer isSolved) {
        SysFamiliarProblem problem = sysFamiliarProblemService.getById(id);
        if (ObjUtil.isNull(problem)) {
            return R.fail("资源不存在");
        }
        LambdaUpdateWrapper<SysFamiliarProblem> wq = new LambdaUpdateWrapper<>();
        if (ObjUtil.equal(isSolved, 0)) {
            wq.set(SysFamiliarProblem::getUnsolved, problem.getUnsolved() + 1);
        } else if (ObjUtil.equal(isSolved, 1)) {
            wq.set(SysFamiliarProblem::getResolved, problem.getResolved() + 1);
        } else {
            return R.fail();
        }
        wq.eq(SysFamiliarProblem::getId, id);
        sysFamiliarProblemService.update(wq);
        //记录用户是否解决
        LambdaQueryWrapper<ConsumerFamiliarProblem> wp = new LambdaQueryWrapper<>();
        wp.eq(ConsumerFamiliarProblem::getProblemId, id);
        wp.eq(ConsumerFamiliarProblem::getUserId, getUserId());
        ConsumerFamiliarProblem one = consumerFamiliarProblemService.getOne(wp);
        if (ObjUtil.isNotNull(one)) {
            return R.ok();
        }

        ConsumerFamiliarProblem conProblem = new ConsumerFamiliarProblem();
        conProblem.setProblemId(id);
        conProblem.setUserId(getUserId());
        conProblem.setResolve(isSolved == 1);
        consumerFamiliarProblemService.save(conProblem);
        return R.ok();
    }

    @ApiOperation("查询列表")
    @GetMapping("list")
    public R<List<SysFamiliarProblem>> findBy() {
//        dto.startPage();
        LambdaQueryWrapper<SysFamiliarProblem> wq = new LambdaQueryWrapper<>();
        wq.eq(SysFamiliarProblem::getSaleable, 1);
        List<SysFamiliarProblem> list = sysFamiliarProblemService.list(wq);
        return R.ok(list);
    }
}
